package controllers

import (
	"github.com/goravel/framework/contracts/database/orm"
	"github.com/goravel/framework/support/carbon"
)

type SearchByParamsService struct {
}

func (s *SearchByParamsService) SearchByParams(params map[string]string) func(methods orm.Query) orm.Query {
	return func(query orm.Query) orm.Query {
		for key, value := range params {
			if key == "action_time" && value != "" {
				//action_time转换为日期，进行等于比较,carbon
				start_time := carbon.Parse(value).ToDateTimeString()
				end_time := carbon.Parse(value).AddDay().ToDateTimeString()
				query = query.Where("action_time >= ? and action_time <= ?", start_time, end_time)
				continue
			}
			if key == "elapsed_time" && value != "" {
				query = query.Where("elapsed_time >= ?", value)
				continue
			}
			if value == "" || key == "pageSize" || key == "total" || key == "currentPage" || key == "sort" || key == "order" {
				continue
			} else {
				query = query.Where(key+" like ?", "%"+value+"%")
			}
		}
		return query
	}
}
